﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Systemet.Views
{
    public partial class PersonalrelateradeKostnader : UserControl
    {
        private Domain.KontoController koControl = new Domain.KontoController();
        private SideViews.SideKostnadsbudgetering cb;
        private Technical.KostnadsbudConnection reg;
        private const int kontoColumn = 0;
        private const int kontoBenämningColumn = 1;
        private const int beloppColumn = 2;

        public PersonalrelateradeKostnader()
        {
            InitializeComponent();
            AccessController.Instance.MainView = this;
        }

        public void updateDgv(object Konton)
        {
            //Sätter columner
            personalRelGV.DataSource = null;
            personalRelGV.DataSource = Konton;
            personalRelGV.Columns[kontoColumn].ReadOnly = true;
            personalRelGV.Columns[kontoBenämningColumn].ReadOnly = true;
            personalRelGV.Columns[beloppColumn].ReadOnly = false;
            


            int sum = 0, intvalue;
            //Räknar ut totalt bvelopp
            foreach (DataGridViewRow row in this.personalRelGV.Rows)
            {
                if (row.Cells[2].Value != null)
                {
                    if (Int32.TryParse(row.Cells[beloppColumn].Value.ToString(), out intvalue))
                    {
                        sum += intvalue;
                        Tbtotalsumma.Text = sum.ToString();
                    }
                }
            }
        }

        public void updateSearchDGV(object konton)
        {
            Tbtotalsumma.Text = "0";
            personalRelGV.DataSource = konton;
            int sum = 0, intvalue;
            
            foreach (DataGridViewRow row in this.personalRelGV.Rows)
            {
                if (row.Cells[2].Visible != false)
                {
                    if (row.Cells[2].Value != null)
                    {
                        if (Int32.TryParse(row.Cells[2].Value.ToString(), out intvalue))
                        {
                            sum += intvalue;
                            Tbtotalsumma.Text = sum.ToString();
                        }
                    }
                }
               
            }
            setColumns();

        }
        //Sätter värden
        public void setColumns()
        {
            personalRelGV.Columns[kontoBenämningColumn].Width = 200;
            personalRelGV.Columns[beloppColumn].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            personalRelGV.Columns[beloppColumn].HeaderText = "Belopp";
            Tbtotalsumma.Location = new Point((personalRelGV.Columns[0].Width + personalRelGV.Columns[1].Width),0);
            Tbtotalsumma.Width = personalRelGV.Columns[2].Width;
        }



        private void dataGridView6_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void PersonalrelateradeKostnader_Load(object Konton, EventArgs e)
        {
   
        }
        //Uppdatera schablon
        private void personalRelGV_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                string headerText = personalRelGV.Columns[e.ColumnIndex].HeaderText;
                if (!headerText.Contains("Belopp")) return;
                string kontonummer = personalRelGV.Rows[e.RowIndex].Cells[0].Value.ToString();
                int schablon = Convert.ToInt32(personalRelGV.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
                updateDgv(koControl.UpdateSchablon(kontonummer.TrimEnd(), schablon));
                cb = (SideViews.SideKostnadsbudgetering)AccessController.Instance.SidMeny;
                cb.UpdateKontoList();
            }
            catch(Exception exx)
            {
            }

        }
        //Fokuserar sig på ett konto
        public void SetFocusOnKonto(string kontoID)
        {
            int i = 0;
            foreach (DataGridViewRow r in personalRelGV.Rows)
            {
                if (r.Cells[0].Value.ToString() == kontoID)
                {
                    r.Selected = true;
                    personalRelGV.FirstDisplayedScrollingRowIndex = r.Index;
                    break;
                }
            }
        }

    }
}
